---
title: Logging and error reporting
version: 1
---

import { Link } from "/src/components/Link";
import { AutoSnippet, When } from "/src/components/CodeSnippet";
import providerObserver from "!!raw-loader!./provider_observer/provider_observer.dart";

Riverpod natively offers a way to listen to all events happening
in the provider tree.  
This can be used to log all the events or to report errors to a
remote service.

This is achieved by using the `ProviderObserver` class,
and passing it to `ProviderScope`/`ProviderContainer`.

## Defining a ProviderObserver

A `ProviderObserver` is a class that should be extended.  
It offers various methods which can be overridden to listen to events:

- `didAddProvider`, called when a provider is added to the tree
- `didUpdateProvider`, called when a provider is updated
- `didDisposeProvider`, called when a provider is disposed
- `providerDidFail`, when a synchronous provider throws an error

<AutoSnippet raw={providerObserver} />

## Using a ProviderObserver

Now that we've defined an observer, we need to use it.  
To do so, we should pass it to either `ProviderScope` or `ProviderContainer`:

```dart
runApp(
  ProviderScope(
    observers: [
      MyObserver(),
    ],
    child: MyApp(),
  )
);
```

```dart
final container = ProviderContainer(
  observers: [
    MyObserver(),
  ],
);
```
